﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShareMarking.Interface;
using System.Security.Cryptography;
using System.Text;
using ShareMarking.Service;
using ShareMarking.Entity;

namespace ShareMarking.Web.Test
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        private string httpstr = "http://my.erpcoo.com/index.php/Api/Qrcode/index";
        private string signsecret = "sdf5sf5et5e42t5";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            #region 2016-06-07 特殊的hospital 和 pnr 二维码

            //#region 新增2个测试医院
            //if (!sfdc_HospitalService.Instance.Get_IsExist_ByWhere(sfdc_Hospital._.Code__c == "H90005"))
            //    {
            //        #region 新增医院
            //        sfdc_Hospital hosp = new sfdc_Hospital()
            //        {
            //            HospitalID = "a3s90000000000EEFF",
            //            Region__c = "华南区",
            //            Area__c = "沈阳区域",
            //            DataType__c = "Hospital",
            //            MSR_StaffID__c = "P905",
            //            Owner__c = "owner905",
            //            Code__c = "H90005",
            //            Name__c = "特殊测试医院905",
            //            Active__c = true,
            //            Sync_Status__c = "已上传",
            //            Sync_Date__c = "2016-02-26T07:37:18.560+0000",
            //            Status__c = "上传成功",
            //            isneedupdate = true
            //        };
            //        sfdc_HospitalService.Instance.Add_Entity(hosp);
            //        ComLib.LogLib.Log4NetBase.Log("1.1.=========添加一条医院数据：：hospcode:H90005");
            //        #endregion
            //    }

            //if (!sfdc_HospitalService.Instance.Get_IsExist_ByWhere(sfdc_Hospital._.Code__c == "H90006"))
            //{
            //    #region 新增医院
            //    sfdc_Hospital hosp = new sfdc_Hospital()
            //    {
            //        HospitalID = "a3s90000000000EE00",
            //        Region__c = "华南区",
            //        Area__c = "沈阳区域",
            //        DataType__c = "Hospital",
            //        MSR_StaffID__c = "P906",
            //        Owner__c = "owner906",
            //        Code__c = "H90006",
            //        Name__c = "特殊测试医院906",
            //        Active__c = true,
            //        Sync_Status__c = "已上传",
            //        Sync_Date__c = "2016-02-26T07:37:18.560+0000",
            //        Status__c = "上传成功",
            //        isneedupdate = true
            //    };
            //    sfdc_HospitalService.Instance.Add_Entity(hosp);
            //    ComLib.LogLib.Log4NetBase.Log("1.1.=========添加一条医院数据：：hospcode:H90006");
            //    #endregion
            //}
            //#endregion

            //#region 医院二维码
            //var hplist = sfdc_HospitalService.Instance.Get_Entity_byWhere(sfdc_Hospital._.isneedupdate == true, null);
            //hplist.ForEach(o =>
            //{
            //    #region 如果在关联表没有hsp对应的记录，就新增二维码
            //    //查询是否有以前弃用的二维码

            //    #region get sceneid
            //    var sceneid = 0;
            //    qr_QrCodeType type = qr_QrCodeTypeService.Instance.Get_SingleEntity_byWhere(qr_QrCodeType._.QrCodeTypeID == 2);
            //    sceneid = type.QT_CurrentIndex.ConvertTo<int>() + 1;
            //    #endregion

            //    #region 编辑图文信息
            //    var extra = new List<extra>() { 
            //        new extra { 
            //            title = "【美赞臣】微信注册", 
            //            shortdesc = "注册成为会员，享受更多优惠折扣！", 
            //            linkurl = string.Format("http://114.55.67.2:8081/WeChatRegister/WeChatRegister?regSubChannel=174$NCCode={0}$PNRCode={1}$openId=####", o.Code__c,o.MSR_StaffID__c),
            //            picurl = "http://svr.meadjohnson.com.cn/mjnwx/css/images/logo_b.png" 
            //        }
            //    };
            //    var message = new message()
            //    {
            //        title = o.Name__c,
            //        remark = o.Name__c + "的二维码",
            //        sacode = "test",
            //        scene_id = sceneid.ToString(),
            //        extra = extra
            //    };
            //    #endregion

            //    #region 请求vigo 生成二维码
            //    var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
            //    var resultentity = vigo.CreateQrcode(message);
            //    #endregion

            //    while (resultentity.errcode == "10")
            //    {
            //        ComLib.LogLib.Log4NetBase.Log("二维码sceneid重复" + sceneid);
            //        sceneid += 1;
            //        message.scene_id = sceneid.ToString();
            //        resultentity = vigo.CreateQrcode(message);
            //    }

            //    #region 将返回的信息保存
            //    if (resultentity.errcode == "00")
            //    {
            //        #region 保存二维码
            //        qr_QrCode qrcode = new qr_QrCode()
            //        {
            //            id = resultentity.message.id,
            //            type = 2,
            //            scene_id = resultentity.message.scene_id,
            //            sacode = resultentity.message.sacode,
            //            title = resultentity.message.title,
            //            remark = resultentity.message.remark,
            //            code_url = resultentity.message.code_url,
            //            qrcode_url = resultentity.message.qrcode_url,
            //            addtime = resultentity.message.addtime,
            //            status = resultentity.message.status,
            //            DateModified = DateTime.Now,
            //            isdelete = false
            //        };
            //        qr_QrCodeService.Instance.Add_Entity(qrcode);
            //        #endregion

            //        #region 添加关联
            //        qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
            //        {
            //            qr_Type = 2,//1是医院
            //            qr_CodeID = resultentity.message.id,
            //            qr_Scene_ID = resultentity.message.scene_id,
            //            rel_Code = o.Code__c,
            //            IsDelete = false,
            //            CreateOn = DateTime.Now
            //        };
            //        qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
            //        #endregion

            //        //更改二维码类型表
            //        type.QT_CurrentIndex = sceneid;
            //        type.QT_Used = type.QT_Used + 1;
            //        type.QT_Surplus = type.QT_Surplus - 1;
            //        qr_QrCodeTypeService.Instance.Update_Entity(type);
            //    }
            //    #endregion

            //    ComLib.LogLib.Log4NetBase.Log("3、====更新医院二维码========新增===Hospital:" + o.Code__c + "::==sceneid==" + resultentity.message.scene_id);

            //    #endregion

            //    #region 将nc置为已修改
            //    //o.isneedupdate = false;
            //    //sfdc_HospitalService.Instance.Update_Entity(o);
            //    #endregion
            //});

            //#endregion

            //#region 新增测试pnr
            //if (!sfdc_PNRService.Instance.Get_IsExist_ByWhere(sfdc_PNR._.EmployeeNumber__c == "P905"))
            //{
            //    sfdc_PNR hosp = new sfdc_PNR()
            //    {
            //        PNRID = "pnr905",
            //        EmployeeNumber__c = "P905",
            //        Region__c = "广东",
            //        Area__c = "广州",
            //        DataType__c = "PNR",
            //        Name__c = "测试P905",
            //        IsActive__c = true,
            //        Sync_Status__c = "已上传",
            //        Sync_Date__c = "2016-02-23T09:39:54.007+0000",
            //        Status__c = "上传成功",
            //        isneedupdate = true
            //    };
            //    sfdc_PNRService.Instance.Add_Entity(hosp);
            //    ComLib.LogLib.Log4NetBase.Log("2.2.====同步PNR数据====新增::P905");
            //}
            //if (!sfdc_PNRService.Instance.Get_IsExist_ByWhere(sfdc_PNR._.EmployeeNumber__c == "P906"))
            //{
            //    sfdc_PNR hosp = new sfdc_PNR()
            //    {
            //        PNRID = "pnr906",
            //        EmployeeNumber__c = "P906",
            //        Region__c = "广东",
            //        Area__c = "广州",
            //        DataType__c = "PNR",
            //        Name__c = "测试P906",
            //        IsActive__c = true,
            //        Sync_Status__c = "已上传",
            //        Sync_Date__c = "2016-02-23T09:39:54.007+0000",
            //        Status__c = "上传成功",
            //        isneedupdate = true
            //    };
            //    sfdc_PNRService.Instance.Add_Entity(hosp);
            //    ComLib.LogLib.Log4NetBase.Log("2.2.====同步PNR数据====新增::P906");
            //}
            //#endregion

            //#region pnr二维码
            ////先得到所有的PNR
            //var pnrlist = sfdc_PNRService.Instance.Get_Entity_byWhere(sfdc_PNR._.isneedupdate == true, null);
            //pnrlist.ForEach(o =>
            //{
            //    #region 如果在关联表没有NC对应的记录，就新增二维码

            //    #region get sceneid
            //    var sceneid = 0;
            //    qr_QrCodeType type = qr_QrCodeTypeService.Instance.Get_SingleEntity_byWhere(qr_QrCodeType._.QrCodeTypeID == 3);
            //    sceneid = type.QT_CurrentIndex.ConvertTo<int>() + 1;
            //    #endregion

            //    #region 编辑图文信息
            //    var extra = new List<extra>() { 
            //        new extra { 
            //            title = "【美赞臣】微信注册", 
            //            shortdesc = "注册成为会员，享受更多优惠折扣！", 
            //            linkurl = "http://114.55.67.2:8081/WeChatRegister/WeChatRegister?regSubChannel=174$PNRCode="+o.EmployeeNumber__c+"$openId=####", 
            //            picurl = "http://svr.meadjohnson.com.cn/mjnwx/css/images/logo_b.png" 
            //        }
            //    };
            //    var message = new message()
            //    {
            //        title = o.Name__c,
            //        remark = o.Name__c + "的二维码",
            //        sacode = "test",
            //        scene_id = sceneid.ToString(),
            //        extra = extra
            //    };
            //    #endregion

            //    #region 请求vigo 生成二维码
            //    var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
            //    var resultentity = vigo.CreateQrcode(message);
            //    #endregion

            //    while (resultentity.errcode == "10")
            //    {
            //        ComLib.LogLib.Log4NetBase.Log("二维码sceneid重复" + sceneid);
            //        sceneid += 1;
            //        message.scene_id = sceneid.ToString();
            //        resultentity = vigo.CreateQrcode(message);
            //    }

            //    #region 将返回的信息保存
            //    if (resultentity.errcode == "00")
            //    {
            //        #region 保存二维码
            //        qr_QrCode qrcode = new qr_QrCode()
            //        {
            //            id = resultentity.message.id,
            //            type = 3,
            //            scene_id = resultentity.message.scene_id,
            //            sacode = resultentity.message.sacode,
            //            title = resultentity.message.title,
            //            remark = resultentity.message.remark,
            //            code_url = resultentity.message.code_url,
            //            qrcode_url = resultentity.message.qrcode_url,
            //            addtime = resultentity.message.addtime,
            //            status = resultentity.message.status,
            //            DateModified = DateTime.Now,
            //            isdelete = false
            //        };
            //        qr_QrCodeService.Instance.Add_Entity(qrcode);
            //        #endregion

            //        #region 添加关联
            //        qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
            //        {
            //            qr_Type = 3,//3是PNR
            //            qr_CodeID = resultentity.message.id,
            //            qr_Scene_ID = resultentity.message.scene_id,
            //            rel_Code = o.EmployeeNumber__c,
            //            IsDelete = false,
            //            CreateOn = DateTime.Now
            //        };
            //        qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
            //        #endregion

            //        //更改二维码类型表
            //        type.QT_CurrentIndex = sceneid;
            //        type.QT_Used = type.QT_Used + 1;
            //        type.QT_Surplus = type.QT_Surplus - 1;
            //        qr_QrCodeTypeService.Instance.Update_Entity(type);

            //        ComLib.LogLib.Log4NetBase.Log("3、====更新PNR二维码========新增===Hospital:" + o.EmployeeNumber__c + "::==sceneid==" + resultentity.message.scene_id);
            //    }
            //    #endregion

            //    #endregion

            //    #region 将nc置为已修改
            //    //o.isneedupdate = false;
            //    //sfdc_PNRService.Instance.Update_Entity(o);
            //    #endregion
            //});
            //#endregion

            #endregion

            #region 多条图文

            //var extra = new List<extra>() { 
            //    new extra {
            //        title = "美赞臣蓝臻，突破性贴近宝宝需求，新妈买一赠一限量臻享", 
            //        picurl="http://qrcode.mjn.howjoin.com/imgforweixin/ac482.jpg",
            //        linkurl = "http://wx.ismartgo.com/app/lighthouse/index.aspx?from=80006$type=2$openid=####",//"http://wx.ismartgo.com/app/lighthouse/index.aspx?from=80006$openid=####"
            //         },
            //    new extra { 
            //        title = "【美赞臣】微信注册", 
            //        //shortdesc = "注册成为会员，享受更多优惠折扣！", 
            //        linkurl = "http://svr.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=80006$openId=####"
            //        //picurl = "http://svr.meadjohnson.com.cn/mjnwx/css/images/logo_b.png"  
            //    }
            //};
            //var message = new message2() { id = "6882", title = "测试多条图文", sacode = "test", remark = "test", extra = extra };

            //Qrcode_vigo vigo = new Qrcode_vigo("http://my.erpcoo.com/index.php/Api/Qrcode/index", "sdf5sf5et5e42t5");
            //var s= vigo.UpdateQrcode(message);

            #endregion

            #region 得到二维码图文
            //Qrcode_vigo vigo = new Qrcode_vigo("http://my.erpcoo.com/index.php/Api/Qrcode/index", "sdf5sf5et5e42t5");
            //var s = vigo.GetWXContent("7734", "");

            //Response.Write(s.message);
            #endregion

            #region 得到扫码次数
            //int i = 1;
            //int testpage = 0;
            //while (i > 0)
            //{
            //    Qrcode_vigo vigo = new Qrcode_vigo("http://my.erpcoo.com/index.php/Api/Qrcode/index", "sdf5sf5et5e42t5");
            //    var qrlog = vigo.GetUsedCount(i);
            //    //testpage = qrlog.message.pageno;
            //    if (qrlog.message.items.Count != 0)
            //    {
            //        var list = qrlog.message.items;
            //        list.ForEach(o =>
            //        {
            //            if (!qr_LogService.Instance.Get_IsExist_ByWhere(qr_Log._.id == o.id))
            //            {
            //                var entity = new qr_Log()
            //                {
            //                    id = o.id,
            //                    codeid = o.codeid,
            //                    codename = o.codename,
            //                    openid = o.openid,
            //                    addtime = o.addtime
            //                };

            //                qr_LogService.Instance.Add_Entity(entity);
            //            }
            //        });

            //        i += 1;
            //    }
            //    else
            //    {
            //        i = 0;
            //    }
            //}


            //Response.Write("完成" + testpage);
            #endregion            
            
            #region 看我们的提供二维码接口                        
            //#region sign
            //var timestamp = DateTime.Now.Ticks - 621355968000000000;
            //var access_id = "accessuser_sfa";
            ////var method = "gethospqrlist";
            //var method = "getpnrqrlist";

            //var parameters = new Dictionary<string, string>();
            //parameters.Add("access_id", access_id);
            //parameters.Add("method", method);
            //parameters.Add("timestamp", timestamp.ToString());

            //var request_str = BuildQuery(parameters);
            //var signature = HMACSHA1Encrypt(request_str, "accessuser_secret_sfa");
            //parameters.Add("sign", signature);
            //#endregion

            //var pst = BuildQuery(parameters).ToString();
            //var postData = Encoding.UTF8.GetBytes(pst);
            //ComLib.WebClientHelper click = new ComLib.WebClientHelper();
            //click.Encoding = Encoding.UTF8;
            //string result = click.Post("http://test.show.howjoin.com/api.ashx", postData);
            //Response.Write(result);

            #endregion
            
            #region 测试


            //var nclist = nc_ncuserService.Instance.Get_Entity_byWhere(
            //    ShareMarking.Entity.nc_ncuser._.nc_code == "2100198" ||
            //    ShareMarking.Entity.nc_ncuser._.nc_code == "302461" ||
            //    ShareMarking.Entity.nc_ncuser._.nc_code == "300331" ||
            //    ShareMarking.Entity.nc_ncuser._.nc_code == "920056" ||
            //    ShareMarking.Entity.nc_ncuser._.nc_code == "930072", null);

            //nclist.ForEach(o =>
            //{
            //    if (o.state == "活跃员工")
            //    {
            //        o.state = "离职员工";
            //        nc_ncuserService.Instance.Update_Entity(o);

            //        var rela = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
            //            qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.rel_Code == o.nc_code);
            //        rela.IsDelete = true;
            //        rela.DateModified = DateTime.Now;
            //        qr_QrCodeRelateService.Instance.db.FromSql(
            //            string.Format("update qr_qrcoderelate set isdelete =1 where qr_type=1 and rel_code='{0}'",
            //            o.nc_code)).Execute();


            //        var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.scene_id == rela.qr_Scene_ID);
            //        qrcode.isdelete = true;

            //        #region 更新二维码，将二维码改成弃用

            //        #region 编辑图文信息
            //        var extra = new List<extra>() { 
            //                    new extra { 
            //                    title = "【美赞臣】微信注册", 
            //                    shortdesc = "注册成为会员，享受更多优惠折扣！", 
            //                    linkurl = "", 
            //                    picurl = "http://svr.meadjohnson.com.cn/mjnwx/css/images/logo_b.png" 
            //                }
            //                };
            //        var message = new message2()
            //        {
            //            id = qrcode.id.ToString(),
            //            title = o.name,
            //            remark = o.name + "的旧二维码",
            //            sacode = o.name + "的旧二维码",
            //            extra = extra
            //        };
            //        #endregion

            //        var vigo = new Qrcode_vigo("http://my.erpcoo.com/index.php/Api/Qrcode/index", "sdf5sf5et5e42t5");
            //        var result = vigo.UpdateQrcode(message);
            //        #endregion
            //    }
            //    else
            //    {
            //        o.state = "活跃员工";
            //        nc_ncuserService.Instance.Update_Entity(o);

            //        var rela = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
            //            qr_QrCodeRelate._.IsDelete == true &&
            //            qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.rel_Code == o.nc_code);
            //        if (rela != null)
            //        {
            //            rela.IsDelete = false;
            //            rela.DateModified = DateTime.Now;
            //            //qr_QrCodeRelateService.Instance.Update_Entity(rela);
            //            qr_QrCodeRelateService.Instance.db.FromSql(
            //            string.Format("update qr_qrcoderelate set isdelete =0 where qr_type=1 and rel_code='{0}'",
            //            o.nc_code)).Execute();

            //            var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.scene_id == rela.qr_Scene_ID);
            //            qrcode.isdelete = false;

            //            #region 更新二维码，将二维码改成启用

            //            #region 编辑图文信息
            //            var extra = new List<extra>() { 
            //                    new extra { 
            //                    title = "【美赞臣】微信注册", 
            //                    shortdesc = "注册成为会员，享受更多优惠折扣！", 
            //                    linkurl = "http://svr.meadjohnson.com.cn/mjnwx/client_phone.action?NCCode="+o.nc_code+"$openId=####", 
            //                    picurl = "http://svr.meadjohnson.com.cn/mjnwx/css/images/logo_b.png" 
            //                }
            //                };
            //            var message = new message2()
            //            {
            //                id = qrcode.id.ToString(),
            //                title = o.name,
            //                remark = o.name + "的二维码",
            //                sacode = o.name + "的二维码",
            //                extra = extra
            //            };
            //            #endregion

            //            var vigo = new Qrcode_vigo("http://my.erpcoo.com/index.php/Api/Qrcode/index", "sdf5sf5et5e42t5");
            //            var result = vigo.UpdateQrcode(message);

            //            #endregion
            //        }
            //        else
            //        {
            //            #region get sceneid
            //            var sceneid = 0;
            //            qr_QrCodeType type = qr_QrCodeTypeService.Instance.Get_SingleEntity_byWhere(qr_QrCodeType._.QrCodeTypeID == 1);
            //            sceneid = type.QT_CurrentIndex.ConvertTo<int>() + 1;
            //            #endregion

            //            #region 编辑图文信息
            //            var extra = new List<extra>() { 
            //            new extra { 
            //                title = "【美赞臣】微信注册", 
            //                shortdesc = "注册成为会员，享受更多优惠折扣！", 
            //                linkurl = "http://svr.meadjohnson.com.cn/mjnwx/client_phone.action?NCCode="+o.nc_code+"$openId=####", 
            //                picurl = "http://svr.meadjohnson.com.cn/mjnwx/css/images/logo_b.png" 
            //            }
            //        };
            //            var message = new message()
            //            {
            //                title = o.name,
            //                remark = o.name + "的二维码",
            //                sacode = "test",
            //                scene_id = sceneid.ToString(),
            //                extra = extra
            //            };
            //            #endregion

            //            #region 请求vigo 生成二维码
            //            var vigo = new Qrcode_vigo("http://my.erpcoo.com/index.php/Api/Qrcode/index", "sdf5sf5et5e42t5");
            //            var resultentity = vigo.CreateQrcode(message);
            //            #endregion

            //            #region 将返回的信息保存
            //            if (resultentity.errcode == "00")
            //            {
            //                #region 保存二维码
            //                qr_QrCode qrcode = new qr_QrCode()
            //                {
            //                    id = resultentity.message.id,
            //                    type = 1,
            //                    scene_id = resultentity.message.scene_id,
            //                    sacode = resultentity.message.sacode,
            //                    title = resultentity.message.title,
            //                    remark = resultentity.message.remark,
            //                    code_url = resultentity.message.code_url,
            //                    qrcode_url = resultentity.message.qrcode_url,
            //                    addtime = resultentity.message.addtime,
            //                    status = resultentity.message.status,
            //                    isdelete = false
            //                };
            //                qr_QrCodeService.Instance.Add_Entity(qrcode);
            //                #endregion

            //                #region 添加关联
            //                qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
            //                {
            //                    qr_Type = 1,//1是NC
            //                    qr_CodeID = resultentity.message.id,
            //                    qr_Scene_ID = resultentity.message.scene_id,
            //                    rel_Code = o.nc_code,
            //                    IsDelete = false,
            //                    CreateOn = DateTime.Now
            //                };
            //                qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
            //                #endregion

            //                //更改二维码类型表
            //                type.QT_CurrentIndex = sceneid;
            //                type.QT_Used = type.QT_Used + 1;
            //                type.QT_Surplus = type.QT_Surplus - 1;
            //                qr_QrCodeTypeService.Instance.Update_Entity(type);
            //            }
            //            #endregion
            //        }
            //    }
            //});
            #endregion
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            #region MyRegion
            //同步每一页的数据
            var httpstrs = "http://moni.mjn.c2d.me/api.ashx?method=getnc";
            ComLib.WebClientHelper click = new ComLib.WebClientHelper();
            click.Encoding = Encoding.UTF8;
            string resultss = click.Post(httpstrs, "");
            var resultentitys = ComLib.JsonHelper.JsonDeserialize<ShareMarking.Interface.ncresults>(resultss);
            try
            {
                #region 写数据库
                var enlit = resultentitys.result.list;
                enlit.ForEach(o =>
                {
                    if (!nc_ncuserService.Instance.Get_IsExist_ByWhere(ShareMarking.Entity.nc_ncuser._.nc_code == o.nc_code))
                    {
                        #region add
                        ShareMarking.Entity.nc_ncuser nc = new ShareMarking.Entity.nc_ncuser()
                        {
                            id = o.id.ToString(),
                            nc_code = o.nc_code,
                            password = o.password,
                            name = o.name,
                            tel = o.tel,
                            email = o.email,
                            state = o.state,
                            nc_nature = o.nc_nature,
                            entr_date = o.entr_date,
                            leave_date = o.leave_date,
                            ncl_code = o.ncl_code,
                            ncl_name = o.ncl_name,
                            ncl_tel = o.ncl_tel,
                            isneedupdate = true
                        };
                        nc_ncuserService.Instance.Add_Entity(nc);
                        #endregion
                        ComLib.LogLib.Log4NetBase.Log("2、====同步NC数据====新增NC:" + o.nc_code);
                    }
                    else
                    {
                        #region update
                        var nc = nc_ncuserService.Instance.Get_SingleEntity_byWhere(
                                ShareMarking.Entity.nc_ncuser._.nc_code == o.nc_code);
                        if (nc.state != o.state)
                        {
                            nc.name = o.name;
                            nc.tel = o.tel;
                            nc.email = o.email;
                            nc.state = o.state;
                            nc.nc_nature = o.nc_nature;
                            nc.entr_date = o.entr_date;
                            nc.leave_date = o.leave_date;
                            nc.ncl_code = o.ncl_code;
                            nc.ncl_name = o.ncl_name;
                            nc.ncl_tel = o.ncl_tel;
                            nc.isneedupdate = true;
                            nc_ncuserService.Instance.Update_Entity(nc);

                            ComLib.LogLib.Log4NetBase.Log("2、====同步NC数据====更新NC:" + o.nc_code + "==状态为:" + o.state);
                        }
                        #endregion
                    }
                });
                #endregion
            }
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("2、====同步NC数据====错误=====" + ex.Message + ex.StackTrace);
            }
            #endregion

            #region MyRegion
            string vigohttp = "http://my.erpcoo.com/index.php/Api/Qrcode/index";
            string signscrect = "sdf5sf5et5e42t5";
            NC_QRCode ncqrsync = new NC_QRCode(vigohttp, signscrect);
            ncqrsync.Sync_QrCode();
            #endregion

            Response.Write("<script>alert('成功同步');</script>");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            #region MyRegion
            var httpstrs = "http://moni.mjn.c2d.me/api.ashx?method=gethosp";
            ComLib.WebClientHelper click = new ComLib.WebClientHelper();
            click.Encoding = Encoding.UTF8;
            string resultss = click.Post(httpstrs, "");
            var resultentitys = ComLib.JsonHelper.JsonDeserialize<ShareMarking.Interface.sfdc_hospresult>(resultss);
            

            var hosplist = resultentitys.hospitalLists;
            hosplist.ForEach(o =>
            {
                if (!sfdc_HospitalService.Instance.Get_IsExist_ByWhere(sfdc_Hospital._.Code__c == o.Hospital_Code__c))
                {
                    #region 新增医院
                    sfdc_Hospital hosp = new sfdc_Hospital()
                    {
                        HospitalID = o.Id,
                        Region__c = o.Hospital_Region__c,
                        Area__c = o.Hospital_Area__c,
                        DataType__c = o.DataType__c,
                        MSR_StaffID__c = o.Hospital_MSR_StaffID__c,
                        Owner__c = o.Hospital_Owner__c,
                        Code__c = o.Hospital_Code__c,
                        Name__c = o.Hospital_Name__c,
                        Active__c = o.Hospital_Active__c,
                        Sync_Status__c = o.Sync_Status__c,
                        Sync_Date__c = o.Sync_Date__c,
                        Status__c = o.Status__c,
                        isneedupdate = true
                    };
                    sfdc_HospitalService.Instance.Add_Entity(hosp);
                    ComLib.LogLib.Log4NetBase.Log("1.1.=========添加一条医院数据：：hospcode:" + o.Hospital_Code__c);
                    #endregion
                }
                else
                {
                    #region 修改
                    var hsp = sfdc_HospitalService.Instance.Get_SingleEntity_byWhere(sfdc_Hospital._.Code__c == o.Hospital_Code__c);
                    if (hsp.Active__c != o.Hospital_Active__c || hsp.MSR_StaffID__c != o.Hospital_MSR_StaffID__c)
                    {
                        hsp.HospitalID = o.Id;
                        hsp.Region__c = o.Hospital_Region__c;
                        hsp.Area__c = o.Hospital_Area__c;
                        hsp.DataType__c = o.DataType__c;
                        hsp.MSR_StaffID__c = o.Hospital_MSR_StaffID__c;
                        hsp.Owner__c = o.Hospital_Owner__c;
                        hsp.Code__c = o.Hospital_Code__c;
                        hsp.Name__c = o.Hospital_Name__c;
                        hsp.Active__c = o.Hospital_Active__c;
                        hsp.Sync_Status__c = o.Sync_Status__c;
                        hsp.Sync_Date__c = o.Sync_Date__c;
                        hsp.Status__c = o.Status__c;
                        hsp.isneedupdate = true;

                        sfdc_HospitalService.Instance.Update_Entity(hsp);
                    }

                    ComLib.LogLib.Log4NetBase.Log("1.1.=========修改一条医院数据：：hospcode:" + o.Hospital_Code__c);
                    #endregion
                }
            });
            #endregion

            #region MyRegion
            string vigohttp = "http://my.erpcoo.com/index.php/Api/Qrcode/index";
            string signscrect = "sdf5sf5et5e42t5";
            SFDC_QRCode ncqrsync = new SFDC_QRCode(vigohttp, signscrect);
            ncqrsync.Sync_HospQrCode();
            #endregion

            Response.Write("<script>alert('成功同步');</script>");
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            #region MyRegion
            var httpstrs = "http://moni.mjn.c2d.me/api.ashx?method=getpnr";
            ComLib.WebClientHelper click = new ComLib.WebClientHelper();
            click.Encoding = Encoding.UTF8;
            string resultss = click.Post(httpstrs, "");
            var resultentitys = ComLib.JsonHelper.JsonDeserialize<ShareMarking.Interface.sfdc_pnrresult>(resultss);

            var pnrlist = resultentitys.PNRLists;
            pnrlist.ForEach(o =>
            {
                if (!sfdc_PNRService.Instance.Get_IsExist_ByWhere(sfdc_PNR._.EmployeeNumber__c == o.PNR_EmployeeNumber__c))
                {
                    sfdc_PNR hosp = new sfdc_PNR()
                    {
                        PNRID = o.Id,
                        EmployeeNumber__c = o.PNR_EmployeeNumber__c,
                        Region__c = o.PNR_Region__c,
                        Area__c = o.PNR_Area__c,
                        DataType__c = o.DataType__c,
                        Name__c = o.PNR_Name__c,
                        IsActive__c = o.PNR_IsActive__c,
                        Sync_Status__c = o.Sync_Status__c,
                        Sync_Date__c = o.Sync_Date__c,
                        Status__c = o.Status__c,
                        isneedupdate = true
                    };
                    sfdc_PNRService.Instance.Add_Entity(hosp);
                    ComLib.LogLib.Log4NetBase.Log("2.2.====同步PNR数据====新增::" + o.PNR_EmployeeNumber__c);
                }
                else
                {
                    #region 修改
                    var pnr= sfdc_PNRService.Instance.Get_SingleEntity_byWhere(sfdc_PNR._.EmployeeNumber__c == o.PNR_EmployeeNumber__c);
                    if (pnr.IsActive__c != o.PNR_IsActive__c)
                    {
                        pnr.PNRID = o.Id;
                        pnr.EmployeeNumber__c = o.PNR_EmployeeNumber__c;
                        pnr.Region__c = o.PNR_Region__c;
                        pnr.Area__c = o.PNR_Area__c;
                        pnr.DataType__c = o.DataType__c;
                        pnr.Name__c = o.PNR_Name__c;
                        pnr.IsActive__c = o.PNR_IsActive__c;
                        pnr.Sync_Status__c = o.Sync_Status__c;
                        pnr.Sync_Date__c = o.Sync_Date__c;
                        pnr.Status__c = o.Status__c;
                        pnr.isneedupdate = true;
                        sfdc_PNRService.Instance.Update_Entity(pnr);
                    }

                    ComLib.LogLib.Log4NetBase.Log("1.1.=========修改一条PNR数据：：pnrcode:" + o.PNR_EmployeeNumber__c);
                    #endregion
                }
            });
            #endregion

            #region MyRegion
            string vigohttp = "http://my.erpcoo.com/index.php/Api/Qrcode/index";
            string signscrect = "sdf5sf5et5e42t5";
            SFDC_QRCode ncqrsync = new SFDC_QRCode(vigohttp, signscrect);
            ncqrsync.Sync_PNRQrCode();
            #endregion

            Response.Write("<script>alert('成功同步');</script>");
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            #region 更新二维码
            var entity = nc_ncuserService.Instance.Get_SingleEntity_byWhere(ShareMarking.Entity.nc_ncuser._.nc_code == "905");
            if (entity != null)
            {
                var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
                    qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
                    && qr_QrCodeRelate._.rel_Code == entity.nc_code);
                if (relate != null)
                {
                    var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
                    if (qrcode != null)
                    {
                        #region 更新二维码

                        #region 编辑图文信息
                        var extra = new List<extra>() { 
                            new extra {
                                    title = "美赞臣蓝臻，突破性贴近宝宝需求，新妈买一赠一限量臻享", 
                                    picurl="http://qrcode.mjn.howjoin.com/imgforweixin/ac419.jpg",
                                    linkurl = "http://wx.ismartgo.com/app/lighthouse/index.aspx?from="+entity.nc_code+"$type=1$openid=####",
                                },
                                new extra { 
                                    title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
                                    shortdesc = "一键注册，终身礼遇", 
                                    linkurl = "http://svr.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+"905"+"$openId=####", 
                                    picurl = "http://admin.mjn.howjoin.com/images/p4.jpg" 
                                },
                                new extra{
                                    title = "不可错过的海量专业孕育课程$胎教早教资源！", 
                                    shortdesc = "有声资源每周更新，满足妈妈$宝宝共同需求", 
                                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
                                    picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
                                },
                                new extra{
                                    title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
                                    shortdesc = "惊喜连连，准备好了么？", 
                                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
                                    picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
                                }
                        };
                        var message = new message2()
                        {
                            id = qrcode.id.ToString(),
                            title = entity.name,
                            remark = entity.name + "多图文",
                            sacode = entity.name + "多图文",
                            extra = extra
                        };
                        #endregion

                        var vigo = new Qrcode_vigo(this.httpstr, this.signsecret);
                        vigo.UpdateQrcode(message);
                        #endregion

                    }
                }
            }
            #endregion                            
        }
        /// <summary>
        /// 组装普通文本请求参数。
        /// </summary>
        /// <param name="parameters">Key-Value形式请求参数字典</param>
        /// <returns>URL编码后的请求数据</returns>
        public static string BuildQuery(IDictionary<string, string> parameters)
        {
            StringBuilder postData = new StringBuilder();
            bool hasParam = false;

            IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
            while (dem.MoveNext())
            {
                string name = dem.Current.Key;
                string value = dem.Current.Value;
                // 忽略参数名为空的参数
                if (!string.IsNullOrEmpty(name))
                {
                    if (hasParam)
                    {
                        postData.Append("&");
                    }

                    postData.Append(HttpUtility.UrlEncode(name, Encoding.UTF8));
                    postData.Append("=");
                    postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
                    hasParam = true;
                }
            }

            return postData.ToString();
        }

        public static string HMACSHA1Encrypt(string EncryptText, string EncryptKey)
        {
            //HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.UTF8.GetBytes(EncryptKey));
            //byte[] RstRes = myHMACSHA1.ComputeHash(Encoding.UTF8.GetBytes(EncryptText));
            //StringBuilder EnText = new StringBuilder();
            //foreach (byte Byte in RstRes)
            //{
            //    EnText.AppendFormat("{0:x2}", Byte);
            //}
            //return EnText.ToString();
            HMACSHA1 hmacsha1 = new HMACSHA1();
            hmacsha1.Key = Encoding.UTF8.GetBytes(EncryptKey);
            byte[] dataBuffer = Encoding.UTF8.GetBytes(EncryptText);
            byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
            return Convert.ToBase64String(hashBytes);
        }
    }
}